package com.ruoyi.web.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SensitiveWords;
import com.ruoyi.system.service.ISensitiveWordsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 敏感词汇管理Controller
 *
 * @author ruoyi
 * @date 2022-09-23
 */
@Controller
@RequestMapping("/system/words")
public class SensitiveWordsController extends BaseController {
    private String prefix = "system/words";

    @Autowired
    private ISensitiveWordsService sensitiveWordsService;

    @RequiresPermissions("system:words:view")
    @GetMapping()
    public String words() {
        return prefix + "/words";
    }

    /**
     * 查询敏感词汇管理列表
     */
    @RequiresPermissions("system:words:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SensitiveWords sensitiveWords) {
        startPage();
        List<SensitiveWords> list = sensitiveWordsService.selectSensitiveWordsList(sensitiveWords);
        return getDataTable(list);
    }

    /**
     * 导出敏感词汇管理列表
     */
    @RequiresPermissions("system:words:export")
    @Log(title = "敏感词汇管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SensitiveWords sensitiveWords) {
        List<SensitiveWords> list = sensitiveWordsService.selectSensitiveWordsList(sensitiveWords);
        ExcelUtil<SensitiveWords> util = new ExcelUtil<SensitiveWords>(SensitiveWords.class);
        return util.exportExcel(list, "敏感词汇管理数据");
    }

    /**
     * 新增敏感词汇管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存敏感词汇管理
     */
    @RequiresPermissions("system:words:add")
    @Log(title = "敏感词汇管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SensitiveWords sensitiveWords) {
        return toAjax(sensitiveWordsService.insertSensitiveWords(sensitiveWords));
    }

    /**
     * 修改敏感词汇管理
     */
    @RequiresPermissions("system:words:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SensitiveWords sensitiveWords = sensitiveWordsService.selectSensitiveWordsById(id);
        mmap.put("sensitiveWords", sensitiveWords);
        return prefix + "/edit";
    }

    /**
     * 修改保存敏感词汇管理
     */
    @RequiresPermissions("system:words:edit")
    @Log(title = "敏感词汇管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SensitiveWords sensitiveWords) {
        return toAjax(sensitiveWordsService.updateSensitiveWords(sensitiveWords));
    }

    /**
     * 删除敏感词汇管理
     */
    @RequiresPermissions("system:words:remove")
    @Log(title = "敏感词汇管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sensitiveWordsService.deleteSensitiveWordsByIds(ids));
    }

    /**
     * 移动端添加
     *
     * @param sensitiveWords 对象
     * @return
     */
    @PostMapping("/myadd")
    @ResponseBody
    public AjaxResult myadd(SensitiveWords sensitiveWords) {
        sensitiveWordsService.insertSensitiveWords(sensitiveWords);
        return success();
    }

    /**
     * 移动端获得敏感词列表
     * @return 敏感词集合
     */
    @PostMapping("/mygetitem")
    @ResponseBody
    public AjaxResult getitem() {
        SensitiveWords sensitiveWords = new SensitiveWords();
        return success(sensitiveWordsService.selectSensitiveWordsList(sensitiveWords));
    }
}
